package com.trkj.service.cf;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.cf.CourseDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.pojo.Course;
import com.trkj.pojo.Coursearrangement;
import com.trkj.pojo.LaborTurnover;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.Traininggactivity;
import com.trkj.service.ServiceException;
import com.trkj.vo.cf.CourseVo;
import com.trkj.vo.cf.CoursearrangementVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class CourseService {
	@Autowired
	private CourseDAO courseDAO;
	@Autowired
	private  StaffInformationDAO staffInformationDAO;
	//分页
			public List<CourseVo> coursePage(String stainName,int page, int rows) throws ServiceException {
				try {
					List<Object[]> list = courseDAO.courseQuery("%"+stainName+"%",new PageRequest(page - 1, rows));
						 List<CourseVo> listvo =new ArrayList<>();
						 for (Object[] pages : list) {
							 Course  cou=  (Course) pages[0];
							 StaffInformation   staff= (StaffInformation) pages[1];
							 CourseVo vo =new CourseVo(); 
							 vo.setCourseId(cou.getCourseId());
							 vo.setStainId(staff.getStainId());
							 vo.setStainName(staff.getStainName());
							 vo.setCourseMakeup(cou.getCourseMakeup());
							 vo.setCourseStartime(cou.getCourseStartime());
							 vo.setCourseEndtime(cou.getCourseEndtime());
							 vo.setCourseState(cou.getCourseState());
							 listvo.add(vo);
						 }
					return listvo;
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(e.getMessage());
				}
			}
			//查询员工
			public List<StaffInformation> coursePageyg() throws ServiceException {
					List<Object[]> map =    courseDAO.findAll3();//coursePageyg1();
					List<StaffInformation> list= new ArrayList<>();
					for (Object[] objects : map) {
						StaffInformation s=	(StaffInformation) objects[0];
						LaborTurnover l=(LaborTurnover) objects[1];
						StaffInformation sta=new StaffInformation();
						sta.setStainId(s.getStainId());
						sta.setStainName(s.getStainName());
						list.add(sta);
					}
					return list;
			}		
			public long pageCount() {
				return courseDAO.count();
			}
			//新增
			public void Courseadd(CourseVo vo)throws ServiceException {
				//System.out.println("llls"+courseId+stainId+courseMakeup+courseState+courseStartime+courseEndtime);
				try {
					StaffInformation staff= staffInformationDAO.findOne(vo.getStainId());
						Course cou=new Course();
						cou.setStaffInformation(staff);
						cou.setCourseMakeup(vo.getCourseMakeup());
						cou.setCourseStartime(new Timestamp(vo.getCourseStartime().getTime()));
						cou.setCourseEndtime(new Timestamp(vo.getCourseEndtime().getTime()));
						cou.setCourseState(vo.getCourseState());
						courseDAO.save(cou);
					} catch (Exception e) {
						e.printStackTrace();
						throw new ServiceException(e.getMessage());
					}
				
			}
			//修改
			public void Courseupdate(CourseVo vo)throws ServiceException {
				try {
				 StaffInformation staff= staffInformationDAO.findOne(vo.getStainId());
					Course cou= courseDAO.findOne(vo.getCourseId());
								cou.setStaffInformation(staff);
								cou.setCourseMakeup(vo.getCourseMakeup());
								cou.setCourseStartime(vo.getCourseStartime());
								cou.setCourseEndtime(vo.getCourseEndtime());
								cou.setCourseState(vo.getCourseState());
								courseDAO.save(cou);
					} catch (Exception e) {
						e.printStackTrace();
						throw new ServiceException(e.getMessage());
					}
			}
			//删除
			public void Courseremove(Long courseId) {
						courseDAO.delete(courseId);
			}
			 
}
